﻿//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="Anurag Chatterjee">
//     Copyright (c) Anurag Chatterjee. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Xml.Schema;

namespace WordsBaseConsole
{
    using System;
    using WordsBase;

    /// <summary>
    /// The program class
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Mains the specified arguments.
        /// </summary>
        /// <param name="args">The arguments.</param>
        public static void Main(string[] args)
        {
            string xmlFileLocation = @"C:\Users\anchat\Desktop\SampleSource.xml";
            IWordProcessor xmlFileProcessor = new XmlFileProcessor(xmlFileLocation);
            ////var root = xmlFileProcessor.GetWordRoot("sparrow");
            ////Console.WriteLine(!string.IsNullOrWhiteSpace(xmlFileProcessor.GetLastErrorMessage())
            ////    ? xmlFileProcessor.GetLastErrorMessage()
            ////    : root);
            ////xmlFileProcessor.AddWordToDerivedList("birds", "peacock");
            ////Console.WriteLine(xmlFileProcessor.GetWordRoot("peacock"));
            ////xmlFileProcessor.AddWordToDerivedList("fishes", "Whale");
            ////Console.WriteLine(xmlFileProcessor.GetWordRoot("Whale"));
            ////Console.WriteLine(xmlFileProcessor.GetLastErrorMessage());
            ////xmlFileProcessor.DeleteWordFromDerivedList("birds", "eagle");
            ////Console.WriteLine(xmlFileProcessor.GetWordRoot("eagle"));
            ////Console.WriteLine(xmlFileProcessor.GetLastErrorMessage());
            ////xmlFileProcessor.UpdateWordInDerivedList("animals", "tiger", "tigress");
            ////Console.WriteLine(xmlFileProcessor.GetWordRoot("tigress"));
            const string BirdsRoot = "birds";
            const string AnimalsRoot = "animals";
            const string VehicleRoot = "Vehicle";
            var wordList1 = new List<string> { "Sparrow", "Mouse", "Dog", "Eagle" };
            var wordList2 = new List<string> { "Woodpecker", "Hornbill", "Elephant", "Car" };
            xmlFileProcessor.AddWordToDerivedList(BirdsRoot, wordList1[0]);
            xmlFileProcessor.AddWordToDerivedList(BirdsRoot, wordList1[3]);
            xmlFileProcessor.AddWordToDerivedList(BirdsRoot, wordList2[0]);
            xmlFileProcessor.AddWordToDerivedList(BirdsRoot, wordList2[1]);
            xmlFileProcessor.AddWordToDerivedList(AnimalsRoot, wordList1[1]);
            xmlFileProcessor.AddWordToDerivedList(AnimalsRoot, wordList1[2]);
            xmlFileProcessor.AddWordToDerivedList(AnimalsRoot, wordList2[2]);
            xmlFileProcessor.AddWordToDerivedList(VehicleRoot, wordList2[3]);

            var comparisonDictionary1 = PopulateComparisonDictionary(wordList1, xmlFileProcessor);
            var comparisonDictionary2 = PopulateComparisonDictionary(wordList2, xmlFileProcessor);

            Console.WriteLine("Distinct roots derived from the first list: {0}. Distinct roots derived from the second list: {1}", comparisonDictionary1.Count, comparisonDictionary2.Count);
            foreach (var element in comparisonDictionary1)
            {
                string rootWord = element.Key;
                Console.WriteLine("Root: " + rootWord);
                foreach (var derivedWord in comparisonDictionary1[rootWord])
                {
                    Console.WriteLine(derivedWord);
                }

                Console.WriteLine("From the second list: ");
                if (comparisonDictionary2.ContainsKey(rootWord))
                {
                    foreach (var derivedWord in comparisonDictionary2[rootWord])
                    {
                        Console.WriteLine(derivedWord);
                    }
                }
            }

            Console.ReadLine();
        }

        /// <summary>
        /// Populates the comparison dictionary.
        /// </summary>
        /// <param name="wordList1">The word list1.</param>
        /// <param name="xmlFileProcessor">The XML file processor.</param>
        /// <returns>The dictionary containing the root values and their derived words</returns>
        private static Dictionary<string, List<string>> PopulateComparisonDictionary(IEnumerable<string> wordList1, IWordProcessor xmlFileProcessor)
        {
            var derivedWordsDictionary = new Dictionary<string, List<string>>();
            foreach (var word in wordList1)
            {
                var wordRoot = xmlFileProcessor.GetWordRoot(word).Key;
                if (!derivedWordsDictionary.ContainsKey(wordRoot))
                {
                    var derivedWordsList = new List<string>();
                    derivedWordsDictionary.Add(wordRoot, derivedWordsList);
                }

                derivedWordsDictionary[wordRoot].Add(word);
            }

            return derivedWordsDictionary;
        }
    }
}
